<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <ul class="button-list">
          <li><span>{{currentCity}}</span></li>
        </ul>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <ul class="button-list">
          <li v-for="item of list" :key="item.id" @click="handleCityClick(item.name)"><span>{{item.name}}</span></li>
        </ul>
      </div>
      <div class="area" v-for="(item,key) of cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <ul class="address-list">
          <li v-for="city of item" :key="city.id" class="border-bottom" @click="handleCityClick(city.name)"><span>{{city.name}}</span></li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
import Bscroll from 'better-scroll'
export default {
  name: 'cityList',
  props: {
    list: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      const element = this.$refs[this.letter]
      this.scroll.scrollToElement(element[0], 300)
      console.log(this.letter)
    }
  },
  methods: {
    handleCityClick (city) {
      // this.$store.dispatch('changeCity', city)
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapActions(['changeCity'])
  }
}
</script>
<style lang="stylus" scoped>
@import "~css/variable.styl"
  .list
    position absolute
    top 1.58rem
    bottom 0
    width 6.4rem
    margin 0 auto
    overflow hidden
  .area
    .title
      line-height .4rem
      background #eee
      padding-left .2rem
      color #666666
      font-size .26rem
    .button-list
      box-sizing border-box
      padding .1rem .6rem .1rem .1rem
      display flex
      align-items center
      flex-wrap wrap
      li
        box-sizing border-box
        flex 0 0 33%
        padding 0 .1rem
        &:nth-child(n+4)
          margin-top .15rem
        span
          display block
          margin 0 auto
          padding .1rem .1rem
          border .02rem solid #ccc
          text-align center
          border-radius .06rem
    .address-list
      padding 0 .2rem
      li
        height .8rem
        line-height .8rem
</style>
